/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.main;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author daz
 */
class SessionManager extends TimerTask {

    private Timer timer;
    private static long duration = 0L;
    private static long delay = 0L;
    private Hashtable users = new Hashtable();
    private long sessionTimeout = 60000;
    private ServerManager server;

    public SessionManager(ServerManager server, Hashtable users, long durationCheck, long sessionTimeout) {
        duration = durationCheck;
        delay = durationCheck;
        this.sessionTimeout = sessionTimeout;
        this.server = server;
        this.users = users;
        timer = new Timer();
        timer.scheduleAtFixedRate(this, delay, duration);
    }

    public void setConfig(long durationCheck, long sessionTimeout) {
        duration = durationCheck;
        delay = durationCheck;
        this.sessionTimeout = sessionTimeout;
        timer.cancel();
        timer.scheduleAtFixedRate(this, delay, duration);
    }

    @Override
    public void run() {
        Session stemp = null;
        Date now = new Date();
        Collection c = null;
        Iterator itr = null;
//        server.logUserStatus("Check Session Timeout", LogDef.LOG_LEVEL.LOG_ALL);
        try {
            c = users.values();
            itr = c.iterator();
            while (itr.hasNext()) {
                stemp = null;
                stemp = (Session) itr.next();
                if (stemp != null) {
                    try {
                        if (((now.getTime() - stemp.dt().getTime()) > sessionTimeout)) {
                            try {
                                //  _server.logUserStatus("Remove client because session timeout", LogDef.LOG_LEVEL.LOG_ALL);
                                users.remove(stemp._id);
                                stemp.client.stop();
                                stemp = null;
                                c = users.values();
                                itr = c.iterator();
                            } catch (Exception ex) {
                                Logger.getLogger(SessionManager.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } catch (Exception ex) {
                        // System.out.println("Remove client because null data");
                        //_server.logUserStatus("Remove client because null data", LogDef.LOG_LEVEL.LOG_ALL);
                        users.remove(stemp._id);
                        stemp.client().stop();
                        stemp = null;
                    }
                    Runtime.getRuntime().gc();
                }
            }
        } catch (Exception ex) {
            //_server.logUserStatus("Error when remove client:" + ex.toString(), LogDef.LOG_LEVEL.LOG_ALL);
        }
    }
}
